package org.example.config;

import com.alibaba.fastjson.JSONObject;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.Alert;
import javafx.scene.control.TableView;
import org.example.Main;
import org.example.controller.JavaProcessManagementController;
import org.example.controller.Loading;
import org.example.dao.ApplicationProcessRepository;
import org.example.dao.PlanRepository;
import org.example.dao.SettingsRepository;
import org.example.model.*;
import org.example.service.FileHandleService;
import org.example.service.StartApplicationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: wwy
 * @Date: 2020/11/26 16:51
 */
@Component
@Order(value = 1)
public class Init implements ApplicationRunner {
    private static final Logger LOGGER = LoggerFactory.getLogger(Init.class);
    @Autowired
    private JavaProcessManagementController javaProcessManagementController;
    @Autowired
    private PlanRepository planRepository;
    @Autowired
    private ApplicationProcessRepository applicationProcessRepository;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<PlanEntity> planEntities = planRepository.findAll();
        ObservableList<Plan> planData = FXCollections.observableArrayList();
        for (PlanEntity planEntity : planEntities) {
            Plan plan1 = new Plan(planEntity.getId(),planEntity.getPlanName(),planEntity.getPriority());
            planData.add(plan1);
        }
        Map<String, ObservableList<ApplicationProcess>> applicationDataMap = JavaProcessManagementController.applicationDataMap;
        javaProcessManagementController.setPlanData(planData);

        for (PlanEntity planEntity : planEntities) {
            ObservableList<ApplicationProcess> applicationProcessData = FXCollections.observableArrayList();
            List<ApplicationProcessEntity> applicationProcessEntityList = applicationProcessRepository.findAllByPlanId(planEntity.getId());
            for (ApplicationProcessEntity applicationProcessEntity : applicationProcessEntityList) {
                ApplicationProcess applicationProcess = new ApplicationProcess(applicationProcessEntity.getId()
                        , applicationProcessEntity.getAppName()
                        ,applicationProcessEntity.getAppAddress()
                        ,""
                        ,0
                        ,""
                        ,applicationProcessEntity.getDefaultName()
                        ,applicationProcessEntity.getListenPort());
                applicationProcessData.add(applicationProcess);

            }
            applicationDataMap.put(planEntity.getId()+"",applicationProcessData);
        }
        LOGGER.info("配置文件初始化成功");

    }
}
